const db = require('../db/index')


// 获取部门消息 参数 id department
exports.getDepartmentMsg = (req, res) => {
  const { id, department } = req.body
  // 根据接收部门获取消息
  const sql = `select * from message where (message_receipt_object = ? or message_receipt_object = '全体成员') and message_status = 0`
  db.query(sql, department, (err, results) => {
    if (err) return res.cc(err)
    // 获取部门消息的全部 id
    let msgArr = []
    results.forEach(item => {
      msgArr.push(item.id)
    })
    // 更新当前用户的 未读列表 read_list 以及 未读状态 read_status
    const sql2 = `update users set read_list = ?, read_status = 1 where id = ?`
    db.query(sql2, [JSON.stringify(msgArr), id], (err, results) => {
      res.send({
        status: 0,
        id: id,
        read_list: msgArr
      })
    })
  })
}

// 获取部门消息列表 参数 department
exports.getDepartmentMsgList = (req, res) => {
  const { department } = req.body
  // 根据接收部门获取消息
  const sql = `select * from message where (message_receipt_object = ? or message_receipt_object = '全体成员')  and message_status = 0`
  db.query(sql, department, (err, results) => {
    if (err) return res.cc(err)
    res.send({
      status: 0,
      message: '获取该部门消息列表成功',
      data: results
    })
  })
}

// 获取该用户未读消息(内容) 参数 id
exports.getUserMsg = (req, res) => {
  const { id } = req.body
  const sql = `select read_list from users where id = ?`
  db.query(sql, id, (err, results) => {
    if (err) return res.cc(err)
    results.forEach(item => {
      let msgArr = JSON.parse(item.read_list)
      const sql2 = `select * from message where id in (?) and message_status = 0`
      db.query(sql2, [msgArr], (err, results) => {
        if (err) return res.cc(err)
        res.send({
          status: 0,
          message: '获取该用户未读消息成功',
          data: results
        })
      })
    })
  })
}

// 获取该用户的未读消息 read_list 和 阅读状态 read_status 参数 id
exports.getReadList = (req, res) => {
  const { id } = req.body
  const sql = `select read_list, read_status from users where id = ?`
  db.query(sql, id, (err, results) => {
    if (err) return res.cc(err)
    res.send({
      status: 0,
      message: '获取该用户的未读消息列表成功',
      data: results[0]
    })
  })
}

// 当用户点击这条消息时, 把当前消息从 read_list中删除 参数 消息的 id 和 用户的 id
exports.readMessage = (req, res) => {
  const sql = `select read_list from users where id = ?`
  db.query(sql, req.body.id, (err, results) => {
    if (err) return res.cc(err)
    // 第一步: 把获取到的 read_list 转化为 JSON 对象
    let read_list = JSON.parse(results[0].read_list)
    // 第二步: 从 read_list 中过滤掉当前消息的 id
    read_list = read_list.filter(item => item != req.body.readId)
    // 第三步: 转化成字符串格式后 更新数据库中的 read_list
    const sql2 = `update users set read_list = ? where id = ?`
    db.query(sql2, [JSON.stringify(read_list), req.body.id], (err, results) => {
      if (err) return res.cc(err)
      res.send({
        status: 0,
        message: '已读成功'
      })
    })
  })
}

// 把新发布的消息 id 插入到当前所属部门的 read_list 中, 参数: readId, department
exports.addDepartmentMsg = (req, res) => {
  const sql = `select read_list, read_status, id from users where department = ?`
  db.query(sql, req.body.department, (err, result) => {
    if (err) return res.cc(err)
    result.forEach(item => {
      if (item.read_status == 1) {
        let msgArr = JSON.parse(item.read_list)
        msgArr.push(JSON.parse(req.body.readId))
        msgArr = JSON.stringify(msgArr)
        const sql2 = `update users set read_list = ? where id = ?`
        db.query(sql2, [msgArr, item.id], (err, result) => { })
      }
    })
    res.send({
      status: 0,
      message: '新增部门消息更新 read_list 成功'
    })
  })
}

// 把新发布的消息 id 从当前所属部门的 read_list 中删除, 参数: readId, department
exports.deleteDepartmentMsg = (req, res) => {
  const sql = `select read_list, read_status, id from users where department = ?`
  db.query(sql, req.body.department, (err, result) => {
    if (err) return res.cc(err)
    result.forEach(item => {
      if (item.read_status == 1) {
        let msgArr = JSON.parse(item.read_list)
        msgArr = msgArr.filter(i => i != req.body.readId)
        msgArr = JSON.stringify(msgArr)
        const sql2 = `update users set read_list = ? where id = ?`
        db.query(sql2, [msgArr, item.id], (err, result) => { })
      }
    })
    res.send({
      status: 0,
      message: '部门消息删除 read_list 成功'
    })
  })
}